/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/

#define _XTAL_FREQ 12000000
#pragma config FOSC = HS, WDT = OFF, CPD=OFF, LVP=OFF


#if defined(__XC)
    #include <xc.h>        /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>       /* HiTech General Include File */
#elif defined(__18CXX)
    #include <p18cxxx.h>   /* C18 General Include File */
#endif

#if defined(__XC) || defined(HI_TECH_C)

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */

#endif

#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"          /* User funct/params, such as InitApp */

#include "lcd.h"

/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/

/* i.e. uint8_t <variable_name>; */

/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/

void showMessage(void);
void showDigit(unsigned char index,unsigned char offset);
void lcd_test1(void);
void LCD_ClearDisplay(void);

unsigned int counter1;


void main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();

    /* Initialize I/O and Peripherals for application */
    InitApp();

    /* TODO <INSERT USER APPLICATION CODE HERE> */

   unsigned char temp=0;
   unsigned char s1,s2;
	 unsigned char displayArray[4];
	 unsigned int counterTemp;

   ADCON1 |= 0x0F;                 // Default all pins to digital
	//GPIO Direction
   //TRISD  = 0;
	TRISB = 0x08;
	TRISE = 0;
	TRISA = 0;

	LCD_LED = 0;

	lcd_initialize();

	LATEbits.LATE0=0;
	LATEbits.LATE1=0;

	RCONbits.IPEN=0;
	INTCONbits.PEIE=1;
	INTCON2bits.INTEDG2=0; //falling edge
	INTCONbits.GIE=1;

	INTCON3bits.INT2IF=0;
	INTCON3bits.INT2IE=1;

	counter1=0;
	//LATEbits.LATE0=1;
	LCD_ClearDisplay();
	lcd_test1();
	
	while(1){
	}

	while(1){
		counterTemp=counter1;
		for (s1=0;s1<2;s1++)	__delay_ms(50);
		displayArray[3]=counterTemp%10;
		counterTemp/=10;
		displayArray[2]=counterTemp%10;
		counterTemp/=10;
		displayArray[1]=counterTemp%10;
		counterTemp/=10;
		displayArray[0]=counterTemp%10;

		showDigit(displayArray[0],0);
		showDigit(displayArray[1],8);
		showDigit(displayArray[2],16);
		showDigit(displayArray[3],24);
	}

}

